﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace BTL
{
    public partial class frmSachcs : Form
    {
        BAL.SACH_BAL db;
        VO.SACH_VO obj;
        SqlConnection conn = new SqlConnection(Core.Connectionstring.Con.ToString());
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        SqlCommand cmd = new SqlCommand();
        public frmSachcs()
        {
            InitializeComponent();
            
            
        }
        private void data()
        {
            obj = new VO.SACH_VO();
            obj.MaS = box_masach.Text;
            obj.TenS = box_tensach.Text;
            obj.Gia = box_gia.Text;
            obj.NXB = box_nhaxuatban.Text;
            obj.NamXB = box_namxuatban.Text;
            obj.SL = box_soluong.Text;
            obj.SlMuon = box_soluongmuon.Text;
        }

        private void Sach_Binding()
        {
            box_masach.DataBindings.Clear();
            box_masach.DataBindings.Add("text", dataGridViewSach.DataSource, "MaS");
            box_tensach.DataBindings.Clear();
            box_tensach.DataBindings.Add("text", dataGridViewSach.DataSource, "TenS");
            box_gia.DataBindings.Clear();
            box_gia.DataBindings.Add("text", dataGridViewSach.DataSource, "Gia");
            box_nhaxuatban.DataBindings.Clear();
            box_nhaxuatban.DataBindings.Add("text", dataGridViewSach.DataSource, "NXB");
            box_namxuatban.DataBindings.Clear();
            box_namxuatban.DataBindings.Add("text", dataGridViewSach.DataSource, "NamXB");
            box_soluong.DataBindings.Clear();
            box_soluong.DataBindings.Add("text", dataGridViewSach.DataSource, "SL");
            box_soluongmuon.DataBindings.Clear();
            box_soluongmuon.DataBindings.Add("text", dataGridViewSach.DataSource, "SlMuon");



        }
        private void bt_Hienthi_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(Core.Connectionstring.Con.ToString());
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            cmd.Connection = conn;
            conn.Open();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from Sach";
            cmd.ExecuteNonQuery();
            da.Fill(dt);
            dataGridViewSach.DataSource = dt;
            conn.Close();


        }

        private void bt_them_Click(object sender, EventArgs e)
        {
            //if (box_masach.Text == "" || box_tensach.Text == "")
            //{
            //    MessageBox.Show("Không Được Để Trống", "Thông Báo!", MessageBoxButtons.OK);
            //}
            //else
            //{
                data();
                db = new BAL.SACH_BAL();
                db.SACH_Insert(obj);
                bt_hienthi_Click_1(sender,e);
                MessageBox.Show("Thêm thành công", "Thông Báo!", MessageBoxButtons.OK);
            //}
        }

        private void bt_sua_Click(object sender, EventArgs e)
        {
            data();
            db = new BAL.SACH_BAL();
            db.SACH_Update(obj);
            dataGridViewSach.DataSource = db;
        }

        private void bt_xoa_Click(object sender, EventArgs e)
        {
            data();
            db.SACH_Delete(obj.MaS);
            dataGridViewSach.DataSource = db;
        }

        private void frmSachcs_Load(object sender, EventArgs e)
        {
            bt_Hienthi_Click(sender, e);
            Sach_Binding();
        }

        private void bt_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "Mã sách")
            {
                SqlConnection conn = new SqlConnection(Core.Connectionstring.Con.ToString());
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                cmd.Connection = conn;
                conn.Open();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select * from Sach where maS like '%" + textBox1.Text.Trim() + "%'";
                cmd.ExecuteNonQuery();
                da.Fill(dt);
                dataGridViewSach.DataSource = dt;
                conn.Close();
            }
            if (comboBox1.Text == "Tên sách")
            {
                
                SqlConnection conn = new SqlConnection(Core.Connectionstring.Con.ToString());
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                cmd.Connection = conn;
                conn.Open();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select * from Sach where tenS like '%" + textBox1.Text.Trim() + "%'";
                cmd.ExecuteNonQuery();
                da.Fill(dt);
                dataGridViewSach.DataSource = dt;
                conn.Close();
            }
        }

        private void bt_hienthi_Click_1(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(Core.Connectionstring.Con.ToString());
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            cmd.Connection = conn;
            conn.Open();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from Sach";
            cmd.ExecuteNonQuery();
            da.Fill(dt);
            dataGridViewSach.DataSource = dt;
            conn.Close();
        }

        private void bt_them_Click_1(object sender, EventArgs e)
        {

            if (box_masach.Text == "" || box_tensach.Text == "")
            {
                MessageBox.Show("Không Được Để Trống", "Thông Báo!", MessageBoxButtons.OK);
            }
            else
            {
            data();
            db = new BAL.SACH_BAL();
            db.SACH_Insert(obj);
            bt_hienthi_Click_1(sender, e);
            MessageBox.Show("Thêm thành công", "Thông Báo!", MessageBoxButtons.OK);
            bt_Hienthi_Click(sender, e);
            Sach_Binding();
            }
        }

        private void bt_sua_Click_1(object sender, EventArgs e)
        {
            bt_sua_Click(sender, e);
            bt_Hienthi_Click(sender, e);
            Sach_Binding();
        }

        private void bt_xoa_Click_1(object sender, EventArgs e)
        {
            bt_xoa_Click_1(sender, e);
            bt_Hienthi_Click(sender, e);
            Sach_Binding();
        }

        private void bt_thoat_Click_1(object sender, EventArgs e)
        {
            if(MessageBox.Show("Thoát","Thoát",MessageBoxButtons.OKCancel)==DialogResult.OK)
            {
                Application.Exit();
            }
        }

        private void frmSachcs_Load_1(object sender, EventArgs e)
        {
            bt_Hienthi_Click(sender, e);
            Sach_Binding();
        }

        private void dataGridViewSach_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.F1)
            {
                frmHelp _help = new frmHelp();
                _help.Show();
            }
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            frmHelp _help = new frmHelp();
            _help.Show();

        }
    }
}
